package org.openwebtop.common.dao;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public abstract class GenericHibernateDAO<T, ID extends Serializable> extends HibernateDaoSupport {
	private Class<T> persistentClass;

	@SuppressWarnings("unchecked")
	public GenericHibernateDAO() {
		persistentClass = (Class<T>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];
	}

	public T findById(ID id) {
		return getHibernateTemplate().get(persistentClass, id);
	}

	@SuppressWarnings("unchecked")
	public List<T> findAll() {
		final DetachedCriteria criteria = DetachedCriteria.forClass(persistentClass);
		return getHibernateTemplate().findByCriteria(criteria);
	}

}
